package com.java2207.group1.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java2207.group1.bo.DinnerBo;
import com.java2207.group1.common.LayuiPageVo;
import com.java2207.group1.mapper.DinnerMapper;
import com.java2207.group1.pojo.Dinner;
import com.java2207.group1.service.DinnerService;
import com.java2207.group1.vo.Breakfast.BarVo;
import com.java2207.group1.vo.Breakfast.PieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author AL 2022/7/27  8:51
 */
@Service("dinnerService")
public class DinnerServiceImpl implements DinnerService {

    @Autowired
    private DinnerMapper dinnerMapper;

    @Override
    public int addDinner(Dinner dinner) {
        return dinnerMapper.addDinner(dinner);
    }

    @Override
    public int deleteDinner(Integer id) {
        return dinnerMapper.deleteDinner(id);
    }

    @Override
    public int updateDinner(Dinner dinner) {
        return dinnerMapper.updateDinner(dinner);
    }

    @Override
    public LayuiPageVo<Dinner> dinnerPageDate(Map<String, Object> params) {
        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");
        //  分页的数据
        PageHelper.startPage(page,limit);
        List<Dinner> dinners = dinnerMapper.selectByExample(params);

        //  mybatis 的分页数据结构
        PageInfo<Dinner> dinnerPageInfo = new PageInfo<>(dinners);

        // 把 mybatis 的分页数据封装到 layui 中
        LayuiPageVo<Dinner> vo = new LayuiPageVo<>();
        vo.setCode(0);
        vo.setMsg("分页列表数据");
        vo.setCount(dinnerPageInfo.getTotal());
        vo.setData(dinnerPageInfo.getList());
        return vo;
    }

    @Override
    public List<BarVo> nameBarData() {
        List<BarVo> list = dinnerMapper.nameBarData();
        return list;
    }

    @Override
    public List<PieVo> namePieData() {
        List<PieVo> list = dinnerMapper.namePieData();
        return list;
    }

    @Override
    public List<DinnerBo> diannerExcelData(Map<String, Object> params) {
        List<Dinner> dinners = dinnerMapper.selectByExample(params);
        List<DinnerBo> dinnerBos =dinners.stream().map((e) -> {
            DinnerBo dinnerBo = new DinnerBo();
            dinnerBo.setId(e.getId());
            dinnerBo.setName(e.getName());
            dinnerBo.setTime(e.getTime());
            dinnerBo.setPic(e.getPic());
            dinnerBo.setSpecial(e.getSpecial());
            dinnerBo.setFruit(e.getFruit());
            return dinnerBo;
        }).collect(Collectors.toList());
        return dinnerBos;
    }


}
